package com.ysu.his.admin.Initialize;

import cn.hutool.json.JSONUtil;
import com.ysu.his.admin.mapper.RoleMapper;
import com.ysu.his.constant.RedisConstant;
import com.ysu.his.entity.user.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class RoleToRedis implements ApplicationRunner
{
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public void run(ApplicationArguments args) throws Exception
    {
        List<Role> allRole = roleMapper.getAllRole();

        allRole.forEach(role -> {
            redisTemplate.opsForHash().put(RedisConstant.ALL_ROLE,String.valueOf(role.getId()),
                                            JSONUtil.toJsonStr(role));
            redisTemplate.expire(RedisConstant.ALL_ROLE,RedisConstant.ROLE_TTL,TimeUnit.MINUTES);
        });
    }
}
